package com.scc.services;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.scc.entites.DishEntity;
import com.scc.entites.dto.DishEntityDTO;
import com.scc.persistence.IAdvancedSearch;
import com.scc.persistence.ICategorySetupDAO;
import com.scc.persistence.IDishSetupDAO;
import com.scc.persistence.LookupFilter;
import com.scc.spring.Validational;

@Service("dishSetupService")
public class DishSetupService implements IDishSetupService
{
   @Autowired
   private IDishSetupDAO dishSetupDAO;
   
   @Autowired
   private ICategorySetupDAO categorySetupDAO;
   
   @Autowired
   IDishSetupAssembler setupAssembler;
   
   @Autowired
   IAdvancedSearch advancedSearch;
   
   @Validational
   @Transactional
   public void createDish(DishEntityDTO incomingDTO)
   {
      DishEntity dish = setupAssembler.assemble(incomingDTO);
      dishSetupDAO.createDish(dish);
   }
   
   @Transactional(readOnly=true)
   public DishEntity getDish(String code)
   {
      DishEntity entity = dishSetupDAO.getDish(code);
      return entity;
   }
   
   @Transactional(readOnly=true)
   public DishEntity getFirst()
   {
      DishEntity entity = dishSetupDAO.getFirst();
      return entity;
   }
   
   @Override
   @Transactional
   public List<DishEntity> advancedSearch(LookupFilter filter)
   {
      return advancedSearch.advancedSearch(filter);
   }

   @Transactional(readOnly=true)
   public List<DishEntity> getAllDishes()
   {
      List<DishEntity> list = dishSetupDAO.getAllDishes();
      Collections.sort(list);
      return list;
   }

   @Transactional
   public void updateDish(String code, String name, String description, int weight)
   {
      //int weight = Integer.valueOf(stringPrice).intValue();
      
      DishEntity fromClient = new DishEntity(code, name, description, weight);
      DishEntity toSave = dishSetupDAO.getDish(code);
      
      updateExistentDish(toSave, fromClient);
      dishSetupDAO.updateDish(toSave);
   }
   
   @Transactional
   public void deleteDish(String code)
   {
      DishEntity entity = dishSetupDAO.getDish(code);
      dishSetupDAO.deleteDish(entity);
   }
   
   private void updateExistentDish(DishEntity toSave,DishEntity fromClient)
   {
      toSave.setDescription(fromClient.getDescription());
      toSave.setName(fromClient.getName());
      toSave.setWeight(fromClient.getWeight());
   }

   public IDishSetupDAO getDishSetupDAO()
   {
      return dishSetupDAO;
   }

   
   public void setDishSetupDAO(IDishSetupDAO dishSetupDAO)
   {
      this.dishSetupDAO = dishSetupDAO;
   }
   
   public ICategorySetupDAO getCategorySetupDAO()
   {
      return categorySetupDAO;
   }

   public void setCategSetupDAO(ICategorySetupDAO categorySetupDAO)
   {
      this.categorySetupDAO = categorySetupDAO;
   }
   
   
}